import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: "",
    username: ""
  },
  mutations: {
    // vuex 存储token
    setToken(state, payload) {
      state.token = payload;
      localStorage.setItem("myToken", payload)
    },
    setUsername(state, payload) {
      state.username = payload;
      localStorage.setItem("setUsername", payload)
    },
    clearToken(){
      localStorage.removeItem("myToken")
    }
  },
  getters: {
    getToken(state) {
      if (state.token) {
        return state.token
      }
      return localStorage.getItem("myToken")
    },
    getUsername(state) {
      if (state.username) {
        return state.username
      }
      return localStorage.getItem("setUsername")
    }
  },
  modules: {
  }
})
